configs annex.post-update-command and annex.pre-commit-command
authorJoey Hess <joeyh@joeyh.name>
Fri, 10 Jan 2025 17:27:51 +0000 (13:27 -0400)
committerJoey Hess <joeyh@joeyh.name>
Fri, 10 Jan 2025 17:27:51 +0000 (13:27 -0400)
Added git configs annex.post-update-command and annex.pre-commit-command
that correspond to the git-annex hook scripts post-update-annex and
pre-commit-annex.

Note that the hook files take precience over the git config, since the git
config can includ global config which should be overridden by local config.

These new git configs are probably not super useful. Especially the
pre-commit-annex hook is there to install scripts to instead of the
pre-commit hook, since git-annex installs that hook itself. So why would
someone want to use a git config for that? Only reason I can think of would
be in a global git config. Or possibly because it's easier to set a git
config than write a hook script, on an OS like Windows.

The real reason I'm adding these is as groundwork for making other
annex.*-command git configs also be available as hook scripts. I want
to avoid having some things available as only git hooks and others as
both gitconfigs and git hooks. (It seems that some annex.*-command configs
don't translate to git hooks though.)

In the man page, moved documentation of the hooks to be next to the
documentation of the git configs. This is to avoid repitition.

Annex/Branch.hs
Annex/Hook.hs
CHANGELOG
Command/PreCommit.hs
Types/GitConfig.hs
doc/git-annex.mdwn

index 945acf724b3e10c9e20ba9f7cc3e632a1478642f..03adce0886b059078a49f5ce4cbd1f979efda4b0 100644 (file)
@@ -257,7 +257,7 @@ updateTo' pairs = do
                        mergeIndex jl refs
                let commitrefs = nub $ fullname:refs
                ifM (handleTransitions jl localtransitions commitrefs)
-                       ( runAnnexHook postUpdateAnnexHook
+                       ( runAnnexHook postUpdateAnnexHook annexPostUpdateCommand
                        , do
                                ff <- if dirty
                                        then return False
@@ -724,7 +724,7 @@ setIndexSha :: Git.Sha -> Annex ()
 setIndexSha ref = do
        f <- fromRepo gitAnnexIndexStatus
        writeLogFile f $ fromRef ref ++ "\n"
-       runAnnexHook postUpdateAnnexHook
+       runAnnexHook postUpdateAnnexHook annexPostUpdateCommand
 
 {- Stages the journal into the index, and runs an action that
  - commits the index to the branch. Note that the action is run
index 8c6d648fb0c8c18dc7a3329a38917318e5ad5b12..deadf871edf93468cc29c632c9910e3d07525475 100644 (file)
@@ -71,18 +71,26 @@ hookWarning h msg = do
 
 {- Runs a hook. To avoid checking if the hook exists every time,
  - the existing hooks are cached. -}
-runAnnexHook :: Git.Hook -> Annex ()
-runAnnexHook hook = do
+runAnnexHook :: Git.Hook -> (GitConfig -> Maybe String) -> Annex ()
+runAnnexHook hook commandcfg = do
        m <- Annex.getState Annex.existinghooks
        case M.lookup hook m of
-               Just True -> run
-               Just False -> noop
+               Just True -> runhook
+               Just False -> runcommandcfg
                Nothing -> do
                        exists <- inRepo $ Git.hookExists hook
                        Annex.changeState $ \s -> s
                                { Annex.existinghooks = M.insert hook exists m }
-                       when exists run
+                       if exists
+                               then runhook
+                               else runcommandcfg
   where
-       run = unlessM (inRepo $ Git.runHook hook) $ do
+       runhook = unlessM (inRepo $ Git.runHook hook) $ do
                h <- fromRepo $ Git.hookFile hook
-               warning $ UnquotedString $ h ++ " failed"
+               commandfailed h
+       runcommandcfg = commandcfg <$> Annex.getGitConfig >>= \case
+               Just command ->
+                       unlessM (liftIO $ boolSystem "sh" [Param "-c", Param command]) $
+                               commandfailed command
+               Nothing -> noop
+       commandfailed c = warning $ UnquotedString $ c ++ " failed"
index b412a2060282da86fea651ad20d9723da664afa8..42bcfa6fb12edcb1d9e531445072f7d61855fed7 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -11,6 +11,9 @@ git-annex (10.20250103) UNRELEASED; urgency=medium
   * git-remote-annex: Use enableremote rather than initremote.
   * Windows: Fix permission denied error when dropping files that
     have the readonly attribute set.
+  * Added git configs annex.post-update-command and annex.pre-commit-command
+    that correspond to the git-annex hook scripts post-update-annex and 
+    pre-commit-annex.
 
  -- Joey Hess <id@joeyh.name>  Fri, 03 Jan 2025 14:30:38 -0400
 
index bc69e4a210884212674d006c7197c122865ae51c..204a5fa8e26234d8ded48f94901d40bce0266f2d 100644 (file)
@@ -44,7 +44,7 @@ seek ps = do
        -- files in the worktree won't be populated, so populate them here
        Command.Smudge.updateSmudged (Restage False)
        
-       runAnnexHook preCommitAnnexHook
+       runAnnexHook preCommitAnnexHook annexPreCommitCommand
 
        -- committing changes to a view updates metadata
        currentView >>= \case
index 2ab5de3ea63e3fc571dc1230cace4f24499789bc..81904653f86f62dcd66e00f5a1d081ca66241734 100644 (file)
@@ -97,6 +97,8 @@ data GitConfig = GitConfig
        , annexAlwaysCompact :: Bool
        , annexCommitMessage :: Maybe String
        , annexCommitMessageCommand :: Maybe String
+       , annexPreCommitCommand :: Maybe String
+       , annexPostUpdateCommand :: Maybe String
        , annexMergeAnnexBranches :: Bool
        , annexDelayAdd :: Maybe Int
        , annexHttpHeaders :: [String]
@@ -190,6 +192,8 @@ extractGitConfig configsource r = GitConfig
        , annexAlwaysCompact = getbool (annexConfig "alwayscompact") True
        , annexCommitMessage = getmaybe (annexConfig "commitmessage")
        , annexCommitMessageCommand = getmaybe (annexConfig "commitmessage-command")
+       , annexPreCommitCommand = getmaybe (annexConfig "pre-commit-command")
+       , annexPostUpdateCommand = getmaybe (annexConfig "post-update-command")
        , annexMergeAnnexBranches = getbool (annexConfig "merge-annex-branches") True
        , annexDelayAdd = getmayberead (annexConfig "delayadd")
        , annexHttpHeaders = getlist (annexConfig "http-headers")
index 5e4c9f477783e766086e9a7bdc9d203048d0935e..1f499cb315078822e02f7c0e0cefcdbc86647b52 100644 (file)
@@ -1155,6 +1155,24 @@ repository, using [[git-annex-config]]. See its man page for a list.)
   This command is run and its output is used as the commit message to the
   git-annex branch.
 
+* `annex.post-update-command`
+
+  This command is run after git-annex updates the git-annex branch.
+
+  Alternatively, a hook script can be installed in 
+  `.git/hooks/post-update-annex`
+
+  When publishing a git-annex repository by http, this can be used to run
+  `git update-server-info`
+
+* `annex.pre-commit-command`
+
+  This command is run whenever a commit is made to the HEAD branch of
+  the git repository, either by git commit, or by git-annex.
+
+  Alternatively, a hook script can be installed in
+  `.git/hooks/pre-commit-annex`
+
 * `annex.alwayscompact`
 
   By default, git-annex compacts data it records in the git-annex branch.
@@ -2322,14 +2340,6 @@ used by git-annex.
 `~/.config/git-annex/autostart` is a list of git repositories
 to start the git-annex assistant in.
 
-`.git/hooks/pre-commit-annex` in your git repository will be run whenever
-a commit is made to the HEAD branch, either by git commit, git-annex
-sync, or the git-annex assistant.
-
-`.git/hooks/post-update-annex` in your git repository will be run
-whenever the git-annex branch is updated. You can make this hook run
-`git update-server-info` when publishing a git-annex repository by http.
-
 # SEE ALSO
 
 More git-annex documentation is available on its web site,